package com.guoguo.aop.spring.advice;

import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;

/**
 * 环绕增强
 */
public class MyMethodInterceptor implements MethodInterceptor {
    /**
     * 环绕增强 这里的方法参数与之前的前置增强、后置增强明显不同，只有一个MethodInvocation类型的参数
     * Implement this method to perform extra treatments before and
     * after the invocation. Polite implementations would certainly
     * like to invoke {@link Joinpoint#proceed()}.
     *
     * @param invocation the method invocation joinpoint
     * @return the result of the call to {@link Joinpoint#proceed()};
     * might be intercepted by the interceptor
     * @throws Throwable if the interceptors or the target object
     *                   throws an exception
     */
    @Override
    public Object invoke(MethodInvocation invocation) throws Throwable {
        System.out.println("==环绕增强开始");
        System.out.println("==方法名：" + invocation.getMethod().getName());
        Object[] args = invocation.getArguments();
        if (null != args && args.length > 0) {
            for (int i = 0; i < args.length; i++) {
                System.out.println("==第" + (i + 1) + "参数：" + args[i]);
            }
        }

        Object proceed = invocation.proceed();

        System.out.println("==环绕增强结束");
        return proceed;
    }
}
